package com.shuai.jit.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.shuai.jit.entity.Party;
import com.shuai.jit.entity.R;
import com.shuai.jit.mapper.PartyMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.parameters.P;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@Slf4j
@RestController
@RequestMapping("/user")
public class PartyController {


    @Autowired
    PartyMapper partyMapper;

    @RequestMapping("/addParty")
    public R addParty(@RequestBody Party party){

        if(party==null||party.getUrl()==null||party.getStep()==null){
            return R.error().data("party","对不起添加失败");
        }
        log.info(party.toString());
        Integer result=partyMapper.insert(party);

        if(result>0){
            return R.ok().data("party","入党手续添加成功");
        }else {
            return R.error().data("party","添加失败");
        }
    }

    @RequestMapping("/selectAllParty")
    public R selectAll(Integer current){
        if(current==null){
            return R.error().data("party","对不起参数为空");
        }
        QueryWrapper<Party> queryWrapper=new QueryWrapper<>();
        // 待审核
        queryWrapper.eq("status",1);

        Page<Party> page=new Page<>(current,10);

        return R.ok().data("party",partyMapper.selectPage(page,queryWrapper));
    }

    @RequestMapping("/updateParty")
    public R update(Long id,Integer status){

        if(id==null||status==null){
            return R.error().data("party","对不起数据为空");
        }

        Party party=partyMapper.selectById(id);

        if(party!=null){
            party.setStatus(status);
            Integer result=partyMapper.updateById(party);

            if(result>0){
                return R.ok().data("party","修改成功");
            }
        }
        return R.error().data("party","对不起入党手续更新失败");
    }
}
